package cn.doctorpda.conference.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.RecomHotInfo;
import cn.doctorpda.conference.bean.ReportInfoItem;
import cn.doctorpda.conference.utils.ImageUtils;
import cn.doctorpda.conference.utils.ScreenUtils;

/**
 * Created by huangjinlong on 2016/5/19.
 */
public class RecommendReportAdapter extends RecyclerView.Adapter<RecommendReportAdapter.ViewHolder> {

    private Context mContext;
    private List<ReportInfoItem> infos = new ArrayList<>();
    private OnItemClickListener listener;
    private int index = 1;
    private String isHome = "";
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, ReportInfoItem info);
    }

    public RecommendReportAdapter() {
    }
    public RecommendReportAdapter(String isHome) {
        this.isHome = isHome;
    }
    public void addAll(List<ReportInfoItem> infos) {
        ++index;
        this.infos.addAll(infos);
        notifyDataSetChanged();
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }


    public void clear() {
        index = 1;
        this.infos.clear();
        notifyDataSetChanged();
    }

    public void add(ReportInfoItem info) {
        this.infos.add(info);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_doc_info, parent, false);
        LinearLayout.LayoutParams la = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(parent.getContext()) / 2, ScreenUtils.getScreenHeight(parent.getContext()) / 4);
        view.setLayoutParams(la);
        this.mContext = parent.getContext();

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, infos.get(position));
                }
            }
        });
        try {
            if (infos != null && infos.size() > 0 && infos.get(position).getImages() != null && infos.get(position).getImages().size() > 0) {
                ImageUtils.loadImage(mContext, infos.get(position).getImages().get(0), holder.mImageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String date_start = "", date_stop = "";
        if (!TextUtils.isEmpty(infos.get(position).getDate())) {
            try {
                date_start = sdf.format(Date.valueOf(infos.get(position).getDate().substring(0, 9)));
//                holder.tvMeetingDate.setText(date_start);
            } catch (Exception e) {
                e.printStackTrace();
                holder.mContent.setText("未知");
            }
        } else {
            holder.mContent.setText("未知");
        }



        holder.mTitle.setText(infos.get(position).getTitle());
        holder.mContent.setText(date_start);
//        holder.mStatus.setText(infos.get(position).getStatus());
    }

    @Override
    public int getItemCount() {
        if (isHome.equals("home")) {
            if (infos.size() > 2) {
                return 2;
            }
        }
        return infos.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImageView;
        private TextView mTitle;
        private TextView mContent;
        private TextView mStatus;

        public ViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.imv_meeting);
            mTitle = (TextView) itemView.findViewById(R.id.txt_item_title);
            mContent = (TextView) itemView.findViewById(R.id.txt_content);
            mStatus = (TextView) itemView.findViewById(R.id.txt_item_state);
            mStatus.setVisibility(View.INVISIBLE);
        }
    }
}
